﻿using Core.MessageEvent;
using IMS.Common.Models;
using IMS.Extensions;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;
using System.Collections.ObjectModel;

namespace IMS.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }

        public IRegionManager regionManager;

        private IRegionNavigationJournal journal;
        private IEventAggregator _aggregator;

        public DelegateCommand GobackCommand { get; private set; }

        public DelegateCommand GoForwardCommand { get; private set; }

        public DelegateCommand ClosingCommand { get; private set; }


        public MainViewModel(IRegionManager regionManager ,IEventAggregator aggregator)
        {
            MenuBars = new ObservableCollection<MenuBar>();
            CreateMenuBar();
            this.NavigateCommand = new DelegateCommand<MenuBar>(Navigate);
            this.regionManager = regionManager;

            GobackCommand = new DelegateCommand(() =>
            {
                if (this.journal == null) return;
                if (this.journal.CanGoBack)
                { this.journal.GoBack(); }
            });

            GoForwardCommand = new DelegateCommand(() =>
            {
                if (this.journal == null) return;
                if (this.journal.CanGoForward)
                { this.journal.GoForward(); }
            });

            _aggregator = aggregator;
            ClosingCommand = new DelegateCommand(Closing);
        }


        private void Navigate(MenuBar bar)
        {
            if (bar == null || string.IsNullOrWhiteSpace(bar.NameSpace)) { return; }
           
            INavigationParameters param = new NavigationParameters
            {
                { bar.NameSpace, "Hello" }
            };
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>
            {
                this.journal = back.Context.NavigationService.Journal;
            }, param);
        }

        private ObservableCollection<MenuBar> menuBars;
        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged();}
        }

        private MenuBar menuItem;
        public MenuBar MenuItem
        {
            get { return menuItem; }
            set { menuItem = value;Navigate(value);}
        }

        void CreateMenuBar()
        {
            MenuBars?.Add(new MenuBar() { Icon = "Home", Title = "首页", NameSpace = "HomePageView" });
            MenuBars?.Add(new MenuBar() { Icon = "NoteboolOutline", Title = "ModBus通讯",NameSpace = "ModBusAllView" });
            MenuBars?.Add(new MenuBar() { Icon = "NotebookPlus", Title = "查看日志", NameSpace = "MessageView" });
            MenuBars?.Add(new MenuBar() { Icon = "Cog", Title = "设置", NameSpace = "SettingView" });
        }

        private void Closing()
        {           
            _aggregator.GetEvent<ClosingEvent>().Publish();
            ContainerLocator.ResetContainer();
        }
    }
}
